<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="classobj.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.classobj.html">类/对象 函数</a></div>
 <div class="up"><a href="book.classobj.html">类/对象</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="classobj.examples" class="appendix">
 <h1>范例</h1>

 <p class="para">
  在这个例子中，我们首先定义了一个基类和该类的扩展。
  这个基类描述了普通的蔬菜，关于它是否可食用及其颜色。
  子类 <var class="varname"><var class="varname">Spinach</var></var> 添加了烹饪的方法和另一个检查是否已烹饪的方法。
 </p>
 <p class="para">
  <div class="example" id="example-4941">
   <p><strong>Example #1 classes.inc</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;base&nbsp;class&nbsp;with&nbsp;member&nbsp;properties&nbsp;and&nbsp;methods<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Vegetable&nbsp;</span><span style="color: #007700">{<br /><br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$edible</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">Vegetable</span><span style="color: #007700">(</span><span style="color: #0000BB">$edible</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">=</span><span style="color: #DD0000">"green"</span><span style="color: #007700">)&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">edible&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$edible</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">color&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$color</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">is_edible</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">edible</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">what_color</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">color</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;end&nbsp;of&nbsp;class&nbsp;Vegetable<br /><br />//&nbsp;extends&nbsp;the&nbsp;base&nbsp;class<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Spinach&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Vegetable&nbsp;</span><span style="color: #007700">{<br /><br />&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$cooked&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">Spinach</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">Vegetable</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">cook_it</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cooked&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">is_cooked</span><span style="color: #007700">()&nbsp;<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">cooked</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;<br />}&nbsp;</span><span style="color: #FF8000">//&nbsp;end&nbsp;of&nbsp;class&nbsp;Spinach<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="para">
  接下来我们从这些类中实例化了两个对象，并打印了他们的信息，包括了他们类的继承关系。
  同时我们也定了一些实用函数，主要为了漂亮地打印出这些变量。
 </p>
 <p class="para">
  <div class="example" id="example-4942">
   <p><strong>Example #2 test_script.php</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
&lt;pre&gt;<br /><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">include&nbsp;</span><span style="color: #DD0000">"classes.inc"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;实用函数<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">print_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)&nbsp;<br />{<br />foreach&nbsp;(</span><span style="color: #0000BB">get_object_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$prop&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\t</span><span style="color: #0000BB">$prop</span><span style="color: #DD0000">&nbsp;=&nbsp;</span><span style="color: #0000BB">$val</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">print_methods</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)&nbsp;<br />{<br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">get_class_methods</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">));<br />foreach&nbsp;(</span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$method</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"\tfunction&nbsp;</span><span style="color: #0000BB">$method</span><span style="color: #DD0000">()\n"</span><span style="color: #007700">;<br />}<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">class_parentage</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #007700">)&nbsp;<br />{<br />if&nbsp;(</span><span style="color: #0000BB">is_subclass_of</span><span style="color: #007700">(</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #0000BB">$obj</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Object&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #DD0000">&nbsp;belongs&nbsp;to&nbsp;class&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">($</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;a&nbsp;subclass&nbsp;of&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Object&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #DD0000">&nbsp;does&nbsp;not&nbsp;belong&nbsp;to&nbsp;a&nbsp;subclass&nbsp;of&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;实例化&nbsp;2&nbsp;对象<br /><br /></span><span style="color: #0000BB">$veggie&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Vegetable</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$leafy&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Spinach</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;打印这些对象的信息<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"veggie:&nbsp;CLASS&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$veggie</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"leafy:&nbsp;CLASS&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$leafy</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">",&nbsp;PARENT&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">get_parent_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$leafy</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;显示蔬菜的属性<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"\nveggie:&nbsp;Properties\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_vars</span><span style="color: #007700">(</span><span style="color: #0000BB">$veggie</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;and&nbsp;leafy&nbsp;methods<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"\nleafy:&nbsp;Methods\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_methods</span><span style="color: #007700">(</span><span style="color: #0000BB">$leafy</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nParentage:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">class_parentage</span><span style="color: #007700">(</span><span style="color: #DD0000">"leafy"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Spinach"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">class_parentage</span><span style="color: #007700">(</span><span style="color: #DD0000">"leafy"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Vegetable"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;/pre&gt;</span>
</code></div>
   </div>

   <div class="example-contents"><p>
    一个重要的东西是注意在上面的例子中，对象 <var class="varname"><var class="varname">$leafy</var></var> 是 <strong class="classname">Spinach</strong> 的实例（<strong class="classname">Vegetable</strong> 的子类），另外脚本的最后部分会输出以下信息：
   </p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
   [...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable
</pre></div>
   </div>
  </div>
 </p>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="classobj.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ref.classobj.html">类/对象 函数</a></div>
 <div class="up"><a href="book.classobj.html">类/对象</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
